package com.cgsd.heyu.ui.webview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;



;
import com.android.library.utils.StringUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.presenter.BasePresenter;
import com.cgsd.heyu.ui.BaseActivity;

/**
 * @author xiaoxing
 */
public class CommonH5Activity extends BaseActivity<BasePresenter> {
    private WebView mWebView;
    private String url;
    /**标题自定义*/
    private boolean isOnPause;
    private String customTitle;

    public static void actionStart(Activity activity, String url, String title){
        if(StringUtils.isEmpty(url)){
            return;
        }
        Intent intent = new Intent(activity, CommonH5Activity.class);
        intent.putExtra("url",url);
        intent.putExtra("title",title);
        activity.startActivity(intent);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_common_webview;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initView() {
        url=getIntent().getStringExtra("url");
        customTitle = getIntent().getStringExtra("title");
        mWebView = findViewById(R.id.mWebView);

        WebSettings webSettings = mWebView.getSettings();
        //是否开启JS支持
        webSettings.setJavaScriptEnabled(true);
        //是否允许JS打开新窗口
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        //缩放至屏幕大小
        webSettings.setUseWideViewPort(true);
        //缩放至屏幕大小
        webSettings.setLoadWithOverviewMode(true);
        //是否支持缩放
        webSettings.setSupportZoom(true);
        //是否支持缩放变焦，前提是支持缩放
        webSettings.setBuiltInZoomControls(true);
        //是否隐藏缩放控件
        webSettings.setDisplayZoomControls(false);
        //是否允许访问文件
        webSettings.setAllowFileAccess(true);
        //是否节点缓存
        webSettings.setDomStorageEnabled(true);
        //是否数据缓存
        webSettings.setDatabaseEnabled(true);
        //是否要手势触发媒体
        webSettings.setMediaPlaybackRequiresUserGesture(false);
        //是否自动加载图片
        webSettings.setLoadsImagesAutomatically(true);
        //设置编码格式
        webSettings.setDefaultTextEncodingName("UTF-8");
        //是否需要获取焦点
        webSettings.setNeedInitialFocus(true);
        //是否从网络获取资源
        webSettings.setBlockNetworkLoads(false);
        //WebView默认不支持同时加载Https和Http混合模式。
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

        mWebView.setBackgroundColor(0);
        mWebView.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //不拦截url
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissCommonSubmitDialog();
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (StringUtils.isNotEmpty(customTitle)){
                    mTitleBar.setTitle(customTitle);
                }else {
                    mTitleBar.setTitle(title);
                }
            }
        });
    }

    @Override
    protected void clickLeft() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        try {
            if (mWebView != null) {
                mWebView.getClass().getMethod("onPause").invoke(mWebView, (Object[]) null);
                isOnPause = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            if (isOnPause) {
                if (mWebView != null) {
                    mWebView.getClass().getMethod("onResume").invoke(mWebView, (Object[]) null);
                }
                isOnPause = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebView.setWebChromeClient(null);
        mWebView.setWebViewClient(null);
        mWebView.getSettings().setJavaScriptEnabled(false);
        mWebView.clearCache(true);

        mWebView.reload ();
        mWebView.removeAllViews();
        mWebView.destroy();
        finish();

    }

    @Override
    public void initData() {
        mWebView.loadUrl(url);
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()){
            mWebView.goBack();
        }else {
            setResult(RESULT_OK);
            finish();
        }
    }
}
